package idds;

import Main.Parameters;
import plugin.SystemInterface;
import java.io.BufferedReader;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import system.MessageOUTPUT;

/**
 * Help for Ice Dragon Delivery System
 * @author zem
 */
public class help implements SystemInterface {

    boolean quit;
    String Next;
    String[] Visited;
    int Position;
    int Length;
    String[] Jump;
    boolean start;
    boolean add;
    int PreviousInputUserLib;

    public void pause() {
    }

    public void play() {
    }

    public void start() { //start showing help

        //save previous number of lib that was using input stream
        PreviousInputUserLib = Parameters.LibConsoleInputReceiver;
        int counter = 0;
        boolean find = false;
        do {
            if (Parameters.RunningLibs[counter].equals("iddsHELP")) {
                find = true;
                Parameters.LibConsoleInputReceiver = counter;
            } else {
                counter++;
            }
        } while (counter < Parameters.RunningLibs.length & !find);



        quit = false;
        Next = "main";
        start = true;
        add = true;
        Visited = new String[10];

        work();

    }

    private void work() {
        if (!quit) {

            ReadParagraf(); //read paragraf
            if (add) { //if need to add to visited list
                AddToVisitedList(Next); //add
            }

        } else {
            Parameters.Libs[Parameters.LibConsoleInputReceiver]=null;
            Parameters.RunningLibs[Parameters.LibConsoleInputReceiver]=null;
            //return number of previous input lib
            Parameters.LibConsoleInputReceiver = PreviousInputUserLib;
        }

    }

    public void interrupt() { //interrupting showing help
        quit = true;
    }

    public void input(String in) { //reading input;
        Next = null;

        try {

            if (in != null && in.length() > 0) {
                if (in.equals("exit") | in.equals("quit")) { //if exit parameter
                    interrupt();
                } else if (in.equals("back")) { //back
                    if (Position > 0) {
                        Position--;
                        Next = Visited[Position];
                        add = false;
                    } else {
                        new MessageOUTPUT().OUTPUT("ev0005", null); //error end of list
                    }
                } else if (in.equals("next")) { //next
                    if (Position < (Length - 1)) {
                        Position++;
                        Next = Visited[Position];
                        add = false;
                    } else {
                        new MessageOUTPUT().OUTPUT("er0004", null); //no next paragrafs
                    }
                } else if (in.equals("main")) { //go to main paragraf
                    Next = "main";
                    add = true;
                } else if (in.equals("help")) { //show help
                    Next = "help";
                    add = true;
                } else if (in.equals("0") | in.equals("1") | in.equals("2") | in.equals("3") | in.equals("4") | in.equals("5") | in.equals("6") | in.equals("7") | in.equals("8") | in.equals("9")) {
                    //looking in-to jump
                    if (Jump[Integer.parseInt(in)] != null) {
                        Next = Jump[Integer.parseInt(in)];
                        add = true;
                    } else {
                        new MessageOUTPUT().OUTPUT("er0005", null);
                    }
                } else {
                    new MessageOUTPUT().OUTPUT("er0005", null);
                }


            }
            work();
        } catch (Exception er) {
        }
    }

    void AddToVisitedList(String paragraf) { //add to visited list
        if (start) { //if help just start it's work
            Visited[0] = paragraf;
            Position = 0;
            Length = 1;
            start = false;
        } else {
            if (Position < 9) { //if position in list less then 9

                Position++;
                Visited[Position] = paragraf;
                Length = Position + 1;

            } else { //position equals 9
                for (int i = 0; i < 9; i++) { //remap visited list
                    Visited[i] = Visited[i + 1];
                }
                Visited[Position] = paragraf;
            }
        }
        add = true;
    }

    void ReadParagraf() { //reading paragraf
        BufferedReader in;
        try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("locale" + File.separatorChar + "help." + Parameters.Language + ".xml"));

            // normalize text representation
            doc.getDocumentElement().normalize();
            NodeList MainPart = doc.getElementsByTagName(Next);
            Node firstNode = MainPart.item(0);
            if (firstNode.getNodeType() == Node.ELEMENT_NODE) {


                Element firstElement = (Element) firstNode;

                boolean flag = true; //mean that everithing is OK

                //----description
                NodeList nodelist = firstElement.getElementsByTagName("message");

                int length = nodelist.getLength();
                new MessageOUTPUT().OutputToCUIWithoutLogging("================================================================================");
                new MessageOUTPUT().OutputToGUI("================================================================================");
                for (int i = 0; i < length; i++) { //printing messages
                    Element nelement = (Element) nodelist.item(i);

                    NodeList textList = nelement.getChildNodes();
                    String temp = ((Node) textList.item(0)).getNodeValue().trim();
                    char[] chars = temp.toCharArray();
                    temp = "";
                    for (int j = 1; j < chars.length - 1; j++) {
                        temp += chars[j];
                    }

                    new MessageOUTPUT().OutputToCUIWithoutLogging(temp);
                    new MessageOUTPUT().OutputToGUI(temp);
                }
                new MessageOUTPUT().OutputToCUIWithoutLogging("================================================================================");
                new MessageOUTPUT().OutputToGUI("================================================================================");
                //checking jump cases
                Jump = new String[10]; //max number of jumps 10
                for (int j = 0; j < 10; j++) {
                    nodelist = firstElement.getElementsByTagName("jump_" + j);
                    length = nodelist.getLength();
                    if (length > 0) {
                        Element nelement = (Element) nodelist.item(0);

                        NodeList textList = nelement.getChildNodes();
                        Jump[j] = ((Node) textList.item(0)).getNodeValue().trim();
                    }
                }
            }
        } catch (Exception er) {
        }
    }
}

